<?php
class Application_Model_invoice extends Application_Model_DomainObject{
	
        protected $_id;//序号
        protected $_billNo;//运单id
        protected $_userid;//用户id 
        protected $_orderdate;//时间
        protected $_company;//公司
        protected $_address;//收件人地址
        protected $_attn;//收件人
        protected $_zipcode;//邮政编码
        protected $_country;//国家
        protected $_invoiceno;//发票编号
        protected $_reason;//理由
        protected $_weight;//重量
        protected $_tel;//电话
        protected $_total;//总价
        protected $_waybillno;//编号
        
        private static function getDb(){
            return new Application_Model_DbTable_invoice();
        }
        
        public static function addinvoice($billno,$userid,$arrre,$arradd)
        {
            $billItem = Application_Model_wayBillItem::bybillNo($billno);
            if($billItem){
                $invoiceItem = Application_Model_invoice::bybillNo($billno);
                if(!$invoiceItem){
                    $invoiceItem = new Application_Model_invoice();
                }

                $invoiceItem->billNo = $billno;
                $invoiceItem->userid = $userid;
                foreach ($arrre as $key => $val){
                    $invoiceItem->$key = $val;
                }
                $invoiceItem->save();
                $billItem->isInvoice = 1;
                $billItem->save();
                for ($index = 0; $index < count($arradd); $index++) {
                    $detailItem = Application_Model_invoicedetail::load($arradd[$index]['did']);
                    if(!$detailItem){
                        $detailItem = new Application_Model_invoicedetail();
                    }
                    $detailItem->billNo = $billno;
                    $detailItem->userid = $userid;
                    foreach ($arradd[$index] as $key => $value) {
                        if($key != "did"){
                            $detailItem->$key = $value;
                        }
                    }
                    if($detailItem->description == "" && $detailItem->num == "" && $detailItem->price == "" && $detailItem->hscode == "" && $detailItem->total == ""){
                        $detailItem->delete();
                    }else{
                        $detailItem->save();  
                    }
                }
                Application_Model_Operate::addOperate(0, null, $billno, "国际运单制作发票", $userid);
                return true;    
            }
        }
        /*
         * 通过运单号查询发票信息
         */
        public static function bybillNo($billno)
        {
            $where[' billNo = '] = $billno;
            return Application_Model_invoice::getDao()->queryOne($where);
        }
        
        public function getDetails()
        {
            return Application_Model_invoicedetail::bybillNo($this->_billNo);
        }
}
?>